using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;

using System.Text;

using System.Web.UI.HtmlControls;
using System.Web.UI;
using DDNY.WebPortals.Common;
using Google.GData.YouTube;
using System.Xml;

using ShopzillaV1.DataAccess;
using ShopzillaV1.Models;

namespace ShopzillaV1.Controllers
{

    public class ProductController : ApplicationController
    {
        //
        // GET: /Product/


        public ActionResult List(string id, string sortby, string page)
        {
            string[] idItems = id.Split(new char[] { '-' });
            if (idItems.Length==1)
            {
                return new PermanentRedirectResult("~/");
            }
            string nodeid = idItems[idItems.GetLength(0) - 2];
            List<string> subcategoriesNames = new List<string>();
            for (int i = 0; i < idItems.GetLength(0); i++)
            {
                if (i % 2 == 1)
                {
                    subcategoriesNames.Add(idItems[i]);
                }

            }
            ViewData[VDConstants.PageTitle] = GenerateTitleFromBrowseNodeNames(subcategoriesNames, sortby, page);

            int pageNumber = 0;
            int.TryParse(page, out pageNumber);
            ProductResponse productsByCat = ShopzillaHelper.GetProductsByCategory(nodeid, pageNumber);
            ViewData[VDConstants.ProductsByCategory] = productsByCat.PagingInfo.Products;
            string categoryName = ShopzillaHelper.GetCategoryNameByCategoryId(nodeid);
            //ViewData[VDConstants.PageTitle] = categoryName;
            ViewData[VDConstants.SubCategories] = ShopzillaHelper.GetSubCategoriesByCategoryId(nodeid);
            ViewData[VDConstants.CurrentProductListActionId] = id;
            PagingModel pagingInfo=new PagingModel();
            pagingInfo.TotalPages = productsByCat.PagingInfo.TotalResults / ShopzillaHelper.PageSize;
            pagingInfo.CurrentPage = pageNumber;
            pagingInfo.TotalResults = productsByCat.PagingInfo.TotalResults;
            ViewData[VDConstants.PagingInfo] = pagingInfo;
            return View("ListByCategory");


        }

        private void AddRssLinkForTopSellers(string nodeid, string searchIndex)
        {
            HtmlLink rss = new HtmlLink();
            rss.Href = "/Feed/TopSellers/" + nodeid + "-" + searchIndex;
            rss.Attributes["type"] = "application/rss+xml";
            rss.Attributes["rel"] = "alternate";
            rss.Attributes["title"] = "Top Sellers in this Category";
            ViewData[VDConstants.TopSellersFeedUrl] = HtmlUtil.RenderControl(rss);
        }

        [AcceptVerbs(new string[] { "Post", "Get" })]
        public ActionResult Search(string id)
        {
            //Items products = AmazonHelper.GetProductsForKeyWord(id);
            return View("ListByNode");
        }


        public ActionResult ProductDetails(string id)
        {
            XmlNode product = ShopzillaHelper.GetProductDetails(id);
            if (product==null)
            {
                return new PermanentRedirectResult("~/");
            }
            string productTitle = product.SelectSingleNode("title").InnerText;

            YouTubeFeed movieFeed = YoutubeHelper.FindVideoByKeyWord(productTitle);
            if (movieFeed.Entries != null && movieFeed.Entries.Count > 0)
            {
                YouTubeEntry entry = movieFeed.Entries[0] as YouTubeEntry;
                
                ViewData[VDConstants.SingleVideoPlayer] = string.Format(ComplexStrings.YouTubeSmallSize, entry.Media.VideoId.Value);
                string videoTitleHtml=string.Format("<p>{0}</p>",entry.Title.Text);
                ViewData[VDConstants.SingleVideoPlayer] = videoTitleHtml + ViewData[VDConstants.SingleVideoPlayer];
            }

            ViewData[VDConstants.PageTitle]=productTitle;
            return View("ProductDetails", product);


        }

        private string GenerateTitleFromBrowseNodeNames(List<string> nodeNames, string sortby, string page)
        {
            StringBuilder title = new StringBuilder();
            title.Append(ViewData[VDConstants.PageTitlePrefix]);
            foreach (var item in nodeNames)
            {
                title.Append(" - ");
                title.Append(item);
            }
            if (nodeNames.Count == 0)
            {
                title.Append("Browse Categories");
            }

            if (!string.IsNullOrEmpty(sortby))
            {
                title.Append(" - Ordered By " + sortby);
            }

            if (!string.IsNullOrEmpty(page))
            {
                title.Append(" - Page " + page);
            }
            return title.ToString();
        }

        //public object Detail(string ASIN)
        //{
        //    //return View("Detail", service.GetProductById(id));
        //}



    }
}
